//格式化时间
export function timeFormat(time,format,full){
    format = format.replace('y',time.getFullYear());
    format = format.replace('m',time.getMonth()+1>9?time.getMonth()+1:((full?'0':'')+(time.getMonth()+1)));
    format = format.replace('d',time.getDate()>9?time.getDate():((full?'0':'')+time.getDate()));
    return format
}

//初始化月数据
export function setMonthData(time){
    let obj = {},
        str_year = time.getFullYear(),
        str_month = time.getMonth()+1,
        time_start = new Date(time.getFullYear()+'/'+(time.getMonth()+1)+'/1'),
        datelen = getdatelen(new Date(time.getFullYear()+'/'+(time.getMonth()+1)+'/1'));

    obj.start = time_start.getDay();
    obj.list = [];
    obj.key = str_year+'/'+str_month;
    for(var i=1;i<=datelen;i++){
        var o = {
            key:obj.key+'/'+i,
            value:i,
            data:[]
        };
        obj.list.push(o);
    }
    return obj;
}

//返回当月天数
export function getdatelen(time){
    time.setMonth(time.getMonth()+1);
    time.setDate(0);
    return time.getDate()
}

export function setChooseDate(date,monthdata,obj){

    var list = monthdata.list,
        date_start = new Date(list[0].key).getTime(),
        date_end = new Date(list[list.length-1].key).getTime(),
        date_choose = new Date(date).getTime();

    if(date_choose>=date_start&&date_choose<=date_end){
        obj['monthObj.list['+loopchoose(date,list)+'].chooseclass'] = 'td-choose';
    }
}

function loopchoose(key,list){
    var idx = -1;
    for(var i=0;i<list.length;i++){
        if(key==list[i].key){
            idx = i;
            break;
        }
    }
    return idx;
}

export function setChooseRange(range,monthdata,obj){

    var list = monthdata.list,
        date_first = new Date(list[0].key).getTime(),
        date_last = new Date(list[list.length-1].key).getTime(),
        date_start = new Date(range[0]).getTime(),
        date_end = new Date(range[1]).getTime(),
        idx_start = -1,
        idx_end = -1,
        in_start = false,
        in_end = false;

    if((date_start>=date_first&&date_start<=date_last)&&date_end>date_last){
        idx_start = loopchoose(range[0],list);
        idx_end = list.length;
        in_start = true;
    }

    if((date_start>=date_first&&date_start<=date_last)&&date_end<=date_last){
        idx_start = loopchoose(range[0],list);
        idx_end = loopchoose(range[1],list);
        in_start = true;
        in_end = true
    }

    if(date_start<date_first&&(date_end>=date_first&&date_end<=date_last)){
        idx_start = 0;
        idx_end = loopchoose(range[1],list);
        in_end = true
    }

    if(date_start<date_first&&date_end>date_last){
        idx_start = 0;
        idx_end = list.length;
    }

    for(var i=idx_start;i<idx_end;i++){
        obj['monthObj.list['+i+'].chooseclass'] = 'td-range';
    }

    if(in_start){
        obj['monthObj.list['+idx_start+'].chooseclass'] = 'td-range td-start';
    }
    if(in_end){
        obj['monthObj.list['+idx_end+'].chooseclass'] = 'td-range td-end';
    }
}

export function clearChooseRange(monthdata,obj){
    var list = monthdata.list;

    for(var i=0;i<list.length;i++){
        if(list[i].chooseclass){
            obj['monthObj.list['+i+'].chooseclass'] = '';
        }
    }
}

